#!/usr/bin/env python3
# -*- coding: utf-8 -*-


from sarena import *
import minimax
import random

class BasicPlayer(Player):

    def __init__(self):
        self.nb_nodes = {} 
        self.time = 0.0
        
    def successors(self, state):
        board, player = state
        actions = board.get_actions() 
        
        for action in actions:
            yield action,(board.clone().play_action(action), -player)
        
    def cutoff(self, state, depth, time_left=1200, time_elapsed=0, step=0):
        board, player = state
        # Count nb of nodes traversed by depth 
        #if str(depth) not in self.nb_nodes.keys() :
        #    self.nb_nodes[str(depth)] = 1
        #else:
        #    self.nb_nodes[str(depth)] += 1             
        if depth>0:
            return True
        else:
            return board.is_finished()
    
    def evaluate(self, state):
        board, player = state
        return board.get_score() 
    

    def play(self, percepts, step, time_left):
        if step % 2 == 0:
            player = -1
        else:
            player = 1
        self.time_left = time_left
        board = Board(percepts)
        state = (board, player)
        return minimax.search(state, self, None, 0)
            
        
    def get_nb_nodes(self):
        return self.nb_nodes.copy()


if __name__ == "__main__":
    player_main(BasicPlayer())
